package com.tjec.project.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.system.domain.BmsServiceArea;
import com.tjec.project.system.service.IBmsServiceAreaService;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * sericeAreaController
 * 
 * @author pangyongfeng
 * @date 2020-10-22
 */
@RestController
@RequestMapping("/system/serviceArea")
public class BmsServiceAreaController extends BaseController
{
    @Autowired
    private IBmsServiceAreaService bmsServiceAreaService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询sericeArea列表
     */
    @PreAuthorize("@ss.hasPermi('system:serviceArea:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsServiceArea bmsServiceArea)
    {
        startPage();
        List<BmsServiceArea> list = bmsServiceAreaService.selectBmsServiceAreaList(bmsServiceArea);
        return getDataTable(list);
    }

    /**
     * 导出sericeArea列表
     */
    @PreAuthorize("@ss.hasPermi('system:serviceArea:export')")
    @Log(title = "sericeArea", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsServiceArea bmsServiceArea)
    {
        List<BmsServiceArea> list = bmsServiceAreaService.selectBmsServiceAreaList(bmsServiceArea);
        ExcelUtil<BmsServiceArea> util = new ExcelUtil<BmsServiceArea>(BmsServiceArea.class);
        return util.exportExcel(list, "serviceArea");
    }

    /**
     * 获取sericeArea详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:serviceArea:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(bmsServiceAreaService.selectBmsServiceAreaByGuid(guid));
    }

    /**
     * 新增sericeArea
     */
    @PreAuthorize("@ss.hasPermi('system:serviceArea:add')")
    @Log(title = "sericeArea", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsServiceArea bmsServiceArea)
    {
    	bmsServiceArea.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    	if(bmsServiceArea.getBusinessCategoryIds()!=null && bmsServiceArea.getBusinessCategoryIds().length>0) {
    		bmsServiceArea.setBusiness(StringUtils.join(bmsServiceArea.getBusinessCategoryIds(),","));
    	}
        return toAjax(bmsServiceAreaService.insertBmsServiceArea(bmsServiceArea));
    }

    /**
     * 修改sericeArea
     */
    //@PreAuthorize("@ss.hasPermi('system:serviceArea:edit')")
    @Log(title = "sericeArea", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsServiceArea bmsServiceArea)
    {
    	bmsServiceArea.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
    	if(bmsServiceArea.getBusinessCategoryIds()!=null && bmsServiceArea.getBusinessCategoryIds().length>0) {
    		bmsServiceArea.setBusiness(StringUtils.join(bmsServiceArea.getBusinessCategoryIds(),","));
    	}
        return toAjax(bmsServiceAreaService.updateBmsServiceArea(bmsServiceArea));
    }

    /**
     * 删除sericeArea
     */
    @PreAuthorize("@ss.hasPermi('system:serviceArea:remove')")
    @Log(title = "sericeArea", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(bmsServiceAreaService.deleteBmsServiceAreaByGuids(guids));
    }
    
    @Log(title = "sericeArea", businessType = BusinessType.OTHER)
    @GetMapping("/checkServiceArea")
    public AjaxResult checkCounts(BmsServiceArea bmsServiceArea) {
    	return AjaxResult.success(bmsServiceAreaService.checkCounts(bmsServiceArea));
    }
}
